package com.zlyx.easy.core.model;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;
import com.zlyx.easy.core.utils.ExceptionUtils;

/**
 * @Auth 赵光
 * @Describle
 * @2019年6月22日 下午10:49:12
 */
public class ResultModel<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final int SUCCESS = 0;

	public static final int FAILURE = 1;

	/**
	 * 响应码
	 */
	private int code;

	/**
	 * 消息内容
	 */
	private String message;

	/**
	 * 响应数据
	 */
	private T data;

	public ResultModel() {
	}

	public T getData() {
		return data;
	}

	public ResultModel<T> setData(T data) {
		this.data = data;
		return this;
	}

	public int getCode() {
		return code;
	}

	public ResultModel<T> setCode(int code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public ResultModel<T> setMessage(String message) {
		this.message = message;
		return this;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}

	public static <T> ResultModel<T> err() {
		return new ResultModel<T>().setCode(FAILURE).setMessage("error");
	}

	public static <T> ResultModel<T> err(Throwable e) {
		return new ResultModel<T>().setCode(FAILURE).setMessage(ExceptionUtils.getExceptionMsg(e));
	}

	public static <T> ResultModel<T> err(String message) {
		return new ResultModel<T>().setCode(FAILURE).setMessage(message);
	}

	public static <T> ResultModel<T> err(int code, String message) {
		return new ResultModel<T>().setCode(code).setMessage(message);
	}

	public static <T> ResultModel<T> failure() {
		return new ResultModel<T>().setCode(FAILURE).setMessage("error");
	}

	public static <T> ResultModel<T> failure(String message) {
		return new ResultModel<T>().setCode(FAILURE).setMessage(message);
	}

	public static <T> ResultModel<T> failure(int code, String message) {
		return new ResultModel<T>().setCode(code).setMessage(message);
	}

	public static <T> ResultModel<T> ok() {
		return new ResultModel<T>().setCode(SUCCESS).setMessage("success");
	}

	public static <T> ResultModel<T> ok(String message) {
		return new ResultModel<T>().setCode(SUCCESS).setMessage(message);
	}

	public static <T> ResultModel<T> ok(int code, String message) {
		return new ResultModel<T>().setCode(code).setMessage(message);
	}

	public static <T> ResultModel<T> success() {
		return new ResultModel<T>().setCode(SUCCESS).setMessage("success");
	}

	public static <T> ResultModel<T> success(T data) {
		return new ResultModel<T>().setCode(SUCCESS).setData(data).setMessage("success");
	}

	public static <T> ResultModel<T> success(int code, T data) {
		return new ResultModel<T>().setCode(code).setData(data).setMessage("success");
	}

	public static <T> ResultModel<T> success(int code, T data, String message) {
		return new ResultModel<T>().setCode(code).setData(data).setMessage(message);
	}
}
